package com.mqr.wallet.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mqr.wallet.common.cache.LocalCache;
import com.mqr.wallet.common.constant.WalletConstants;
import com.mqr.wallet.common.exception.BaseException;
import com.mqr.wallet.domain.WalletInfo;
import com.mqr.wallet.domain.WalletTransaction;
import com.mqr.wallet.factory.CashWithdrawalServiceFactory;
import com.mqr.wallet.mapper.WalletTransactionMapper;
import com.mqr.wallet.service.WalletInfoService;
import com.mqr.wallet.service.WalletTransactionService;
import com.mqr.wallet.vo.WalletVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <B>主类名称：</B>WalletTransactionServiceImpl<BR>
 * <B>概要说明：</B>钱包流水服务实现类<BR>
 *
 * @author mqr
 * @since 2023年2月21日 下午12:52:50
 */
@Service
public class WalletTransactionInfoServiceImpl implements WalletTransactionService {

    /**
     * 钱包流水信息mapper组件
     */
    @Autowired(required = false)
    private WalletTransactionMapper walletTransactionMapper;


    /**
     * 初始化阶段将用户信息加载到缓存中
     */
    @PostConstruct
    public void init() {
        // 初始化加载用户流水信息
        cacheWalletTransaction(null);
    }


    /**
     * 新增钱包流水记录
     *
     * @param walletVO
     */
    @Override
    public void addWalletTransaction(WalletVO walletVO) {
        WalletTransaction walletTransaction = new WalletTransaction();
        walletTransaction.setUserId(walletVO.getUserId());
        walletTransaction.setAmount(walletVO.getAmount());
        walletTransaction.setBalance(walletVO.getWalletInfo().getBalance());
        walletTransaction.setOperatorType(walletVO.getOperatorType());
        walletTransaction.setResource(walletVO.getResource());
        int status = WalletConstants.NUM_1;
        if (StringUtils.isNotBlank(walletVO.getCashBankType())) {
            // 根据支出提现响应状态判断交易状态
            status = walletVO.getCashResult().getCode().equals(WalletConstants.SUCCESS) ? WalletConstants.NUM_1 : WalletConstants.NUM_0;
        }

        walletTransaction.setStatus(status);
        walletTransaction.setRemark(walletVO.getRemark());
        walletTransactionMapper.insert(walletTransaction);
        // 更新缓存
        cacheWalletTransaction(walletTransaction.getUserId());
    }

    /**
     * 根据用户id获取钱包流水信息
     *
     * @param userId
     * @return 钱包流水信息列表
     */
    @Override
    public List<WalletTransaction> findByUserId(Integer userId) {
        // 从缓存取用户钱包信息
        return LocalCache.wallerTransactionCache.getIfPresent(userId);
    }


    /**
     * 缓存用户钱包流水信息
     */
    private void cacheWalletTransaction(Integer userId) {
        LambdaQueryWrapper<WalletTransaction> queryWrapper = new LambdaQueryWrapper<WalletTransaction>();
        if (null != userId) {
            queryWrapper.eq(WalletTransaction::getUserId, userId);
        }
        List<WalletTransaction> list = walletTransactionMapper.selectList(queryWrapper);
        Map<Integer, List<WalletTransaction>> resultMap = list.stream().collect(Collectors.groupingBy(WalletTransaction::getUserId));
        resultMap.keySet().forEach(e -> {
            LocalCache.wallerTransactionCache.put(e, resultMap.get(e));
        });
    }


}
